<template>
  <div>
    <van-pull-refresh v-model="isRefresh" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <article-item
          v-for="(item, index) in articleList"
          :key="index"
          :article="item"
        >
        </article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem/ArticleItem.vue'
import { getArtListAPI } from '@/api/homeAPI.js'
export default {
  name: 'ArticleList',
  data() {
    return {
      articleList: [],
      isRefresh: false,
      loading: false,
      finished: false
    }
  },
  props: ['channelId'],

  components: {
    ArticleItem
  },

  computed: {},

  mounted() {
    this.initArtList()
  },

  methods: {
    async initArtList(state) {
      const { data: res } = await getArtListAPI(this.channelId)
      if (res.message === 'OK') {
        if (state) {
          this.articleList = [...res.data.results, ...this.articleList]
          // 下拉数据加载完毕，关闭下拉loading
          this.isRefresh = false
        } else {
          this.articleList = [...this.articleList, ...res.data.results]
          this.loading = false
          if (res.data.results.length === 0) {
            this.finished = true
          }
        }
      }
    },
    onRefresh() {
      console.log('你在下拉')
      this.initArtList(true)
    },
    onLoad() {
      this.initArtList()
    }
  }
}
</script>
<style lang="less" scoped></style>
